﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;

namespace BTVPodcatcherNamespace
{
    
    public partial class NEWDownloadViewer : Form
    {
        public NEWDownloadViewer()
        {
            InitializeComponent();
            
            
        }
        public void setupForm()
        {
            //dowloadInfoBindingSource.DataSource = ((MainForm)this.MdiParent).allDownloadingItems;
            flowLayoutPanel1.Controls.Clear();
            foreach (DowloadInfo di in ((MainForm)this.MdiParent).allDownloadingItems)
            {
                flowLayoutPanel1.Controls.Add(new DownloadInfoControl(di, ((MainForm)this.MdiParent)));
                
                flowLayoutPanel1.SetFlowBreak(flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1], true);
                flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Size = new System.Drawing.Size(flowLayoutPanel1.Size.Width-25, flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Size.Height);
                flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top )
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
                
            }
            ((MainForm)this.MdiParent).downloadAdded += new MainForm.DownloadAddedDelegate(NEWDownloadViewer_downloadAdded);
            ((MainForm)this.MdiParent).downloadRemovedEvent += new MainForm.DownloadRemovedDelegate(NEWDownloadViewer_downloadRemoved);
        }
        private delegate void NEWDownloadViewer_downloadRemovedDelgagte(object sender, EventArgs e);
        void NEWDownloadViewer_downloadRemoved(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                this.Invoke(new NEWDownloadViewer_downloadRemovedDelgagte(NEWDownloadViewer_downloadRemoved), new object[] { sender, e });
            }
            else
            {
                try
                {
                    List<Control> ControlsToRemove = new List<Control>();
                    foreach (Control c1 in flowLayoutPanel1.Controls)
                    {
                        if (c1 is DownloadInfoControl)
                        {
                            DowloadInfo di = ((DownloadInfoControl)c1).downloadJob;
                            if (!((MainForm)this.MdiParent).allDownloadingItems.Contains(di))
                            {
                                ControlsToRemove.Add(c1);
                            }

                        }
                    }
                    foreach (Control c1 in ControlsToRemove)
                    {
                        flowLayoutPanel1.Controls.Remove(c1);
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        private delegate void NEWDownloadViewer_downloadAddeddelgate(object sender, DownloadInfoEventArgs e);
     
        void NEWDownloadViewer_downloadAdded(object sender, DownloadInfoEventArgs e)
        {
            if (flowLayoutPanel1.InvokeRequired)
            {
                flowLayoutPanel1.Invoke(new NEWDownloadViewer_downloadAddeddelgate(NEWDownloadViewer_downloadAdded), new object[] { sender, e });
            }
            else
            {

                flowLayoutPanel1.Controls.Add(new DownloadInfoControl(e.di, ((MainForm)this.MdiParent)));

                flowLayoutPanel1.SetFlowBreak(flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1], true);
                flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Size = new System.Drawing.Size(flowLayoutPanel1.Size.Width - 10, flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Size.Height);
                flowLayoutPanel1.Controls[flowLayoutPanel1.Controls.Count - 1].Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {  
            //try
            //{
            //    int i =dataGridView1.FirstDisplayedScrollingRowIndex;
            //    DataGridViewSelectedRowCollection selected = dataGridView1.SelectedRows;
            //    dowloadInfoBindingSource.ResetBindings(false);
            //    if (i<dataGridView1.RowCount&&i>=0)
            //    {
            //        dataGridView1.FirstDisplayedScrollingRowIndex = i;
            //    }
            
            //    foreach (DataGridViewRow dgv in selected)
            //    {
            //        dgv.Selected = true;
            //    }
            //}
            //catch (Exception)
            //{

            //}

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    if (dowloadInfoBindingSource.Current != null)
            //    {
            //        if (((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus == DownloadStatus.Downloading || ((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus == DownloadStatus.Ready)
            //            ((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus = DownloadStatus.CancelRequested;
            //    }
            //}
            //catch (Exception)
            //{
            //}

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //try
            //{

            //    if (dowloadInfoBindingSource.Current != null)
            //    {
            //        ((DowloadInfo)dowloadInfoBindingSource.Current).DownloadStatus = DownloadStatus.CancelRequested;
            //        ((MainForm)this.MdiParent).allDownloadingItems.Remove(((DowloadInfo)dowloadInfoBindingSource.Current));
            //    }
            //}
            //catch (Exception)
            //{
            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Control c1 in flowLayoutPanel1.Controls)
                {
                    if (c1 is DownloadInfoControl)
                    {
                        DowloadInfo di = ((DownloadInfoControl)c1).downloadJob;
                        if (di.DownloadStatus == DownloadStatus.Downloading || di.DownloadStatus == DownloadStatus.Ready)
                            di.DownloadStatus = DownloadStatus.CancelRequested;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                while (((MainForm)this.MdiParent).allDownloadingItems.Count > 0)
                {

                    ((MainForm)this.MdiParent).allDownloadingItems[0].DownloadStatus = DownloadStatus.CancelRequested;
                    ((MainForm)this.MdiParent).allDownloadingItems.Remove(((MainForm)this.MdiParent).allDownloadingItems[0]);
                    
                }

                ((MainForm)this.MdiParent).downloadRemoved();
                
            }
            catch (Exception e2)
            {
                ConsoleForm.addMessage("Error " + e2.ToString());
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
            //{
            //    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            //}
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            System.Console.WriteLine(e.Exception.ToString());
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //if (e.Value != null)
            //{
            //    if (dataGridViewTextBoxColumn3.DisplayIndex == e.ColumnIndex && e.Value != null)
            //    {
            //        e.Value = string.Format("{0:#,###,###} kBps", (((long)e.Value) / 1024.0));
            //        e.FormattingApplied = true;
            //    }
            //    else if (dataGridViewTextBoxColumn5.DisplayIndex == e.ColumnIndex)
            //    {
            //        if (e.Value.Equals(DownloadStatus.CancelRequested))
            //        {
            //            e.Value = "Cancel Requested";
            //            e.FormattingApplied = true;
            //        }
            //        else if (e.Value.Equals(DownloadStatus.PropertiesWritten))
            //        {
            //            e.Value = "BTV Properties Written";
            //            e.FormattingApplied = true;
            //        }
            //        else if (e.Value.Equals(DownloadStatus.WaitingToWriteProperties))
            //        {
            //            e.Value = "Waiting to Write BTV Properties";
            //            e.FormattingApplied = true;
            //        }
            //        else if (e.Value.Equals(DownloadStatus.FailedToWriteProperties))
            //        {
            //            e.Value = "Failed to Write BTV Properties";
            //            e.FormattingApplied = true;
            //        }
            //    }
            //}
            
        }

        private void writeBTVMediaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if (dowloadInfoBindingSource.Current != null)
            //{
            //    DowloadInfo di = ((DowloadInfo)dowloadInfoBindingSource.Current);
            //    di.DownloadStatus = DownloadStatus.WaitingToWriteProperties;
            //    BTVWriter btw = new BTVWriter(di, (MainForm)this.MdiParent);

            //    System.Threading.Thread btvWriter = new System.Threading.Thread(new System.Threading.ThreadStart(btw.writeBTVProprties));
            //    btvWriter.Name = "BTV Podcatcher Media Tag Writer";
            //    btvWriter.IsBackground = true;
            //    btvWriter.Start();
            //}
        }

        private void contextMenuStrip1_Opened(object sender, EventArgs e)
        {
            //writeBTVMediaToolStripMenuItem.Visible = false;
            //if (dowloadInfoBindingSource.Current != null)
            //{
            //    DowloadInfo di = ((DowloadInfo)dowloadInfoBindingSource.Current);
            //    if (di.DownloadStatus == DownloadStatus.Complete || di.DownloadStatus == DownloadStatus.PropertiesWritten || di.DownloadStatus == DownloadStatus.Error || di.DownloadStatus == DownloadStatus.FailedToWriteProperties)
            //    {
            //        writeBTVMediaToolStripMenuItem.Visible = true;
            //    }
            //}
        }

        private void flowLayoutPanel1_SizeChanged(object sender, EventArgs e)
        {
            foreach (Control c1 in flowLayoutPanel1.Controls)
            {
                c1.Size = new System.Drawing.Size(flowLayoutPanel1.Size.Width - 25, c1.Size.Height);
            }
        }

        
    }
}
